package com.shaomengjie.meetingroom;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.shaomengjie.meetingroom.fragment.LeftFragment;
import com.shaomengjie.meetingroom.fragment.MiddleFragment;
import com.shaomengjie.meetingroom.fragment.RightFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    TabHost tabHost = null;
    List<Fragment> mFragmentList = new ArrayList<>();
    TextView textView_toolbar ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();

    }

    private void initData() {
        mFragmentList.add(new LeftFragment());
        mFragmentList.add(new MiddleFragment());
        mFragmentList.add(new RightFragment());
    }

    private void initView() {
        tabHost = (TabHost) findViewById(R.id.tabhost);
        textView_toolbar = (TextView) findViewById(R.id.toolbar_title);
        tabHost.setup();
        for (int i = 0; i < 3; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.myindicator, null, false);
            TextView textView = (TextView) view.findViewById(R.id.tv_indicator);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_indicator);

            switch (i) {
                case 0:
                    textView.setText("第一个");
                    imageView.setImageResource(R.mipmap.ic_launcher);
                    tabHost.addTab(tabHost.newTabSpec("1").setIndicator(view).setContent(R.id.f_one));
                    getSupportFragmentManager().beginTransaction().replace(R.id.f_one,mFragmentList.get(0)).commit();
                    textView_toolbar.setText("第一个");
                    break;
                case 1:
                    textView.setText("第二个");
                    imageView.setImageResource(R.mipmap.ic_launcher);
                    tabHost.addTab(tabHost.newTabSpec("2").setIndicator(view).setContent(R.id.f_two));
                    getSupportFragmentManager().beginTransaction().replace(R.id.f_two,mFragmentList.get(1)).commit();
                    break;
                case 2:
                    textView.setText("第三个");
                    imageView.setImageResource(R.mipmap.ic_launcher);
                    tabHost.addTab(tabHost.newTabSpec("3").setIndicator(view).setContent(R.id.f_three));
                    getSupportFragmentManager().beginTransaction().replace(R.id.f_three,mFragmentList.get(2)).commit();

                    break;
            }

            tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String tabId) {
                    for (int i = 0; i < 3; i++) {
                        ((TextView) tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.black));
                    }
                    if (tabHost.getCurrentTabTag() == tabId) {
                        ((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.red));

                    }
                    if ("1".equals(tabId)){
                        textView_toolbar.setText("第一个");
                    }
                    if ("2".equals(tabId)){
                        textView_toolbar.setText("第二个");
                    }
                    if ("3".equals(tabId)){
                        textView_toolbar.setText("第三个");
                    }
                }
            });
            ((TextView) tabHost.getCurrentTabView().findViewById(R.id.tv_indicator)).setTextColor(getResources().getColor(R.color.red));

        }

    }

    public void add(){


        
    }
}
